package TrainModel;

import Utilities.logFileIO;
import Utilities.TrainConstants;

public class TrainCar
{	
	//private int iIDNum;
	private int iNumPassengers;
	private String logFileHeader;
	public boolean bConductorOnBoard;
	private boolean bLightsOn;
	private boolean bDoorsOpen;
	
	private double dLength;
	private double dWidth;
	private double dHeight;
	//private double dBaseWeight; 
	private double dTotalWeight;
	private double dPassengerWeight;
	
	private double dThermostat;
	
	private String sCurrentAnnouncement;
	private String sCurrentAdvert;
	int iAdvertCount;
	int iAnnouncmentCount;
	
	private logFileIO logFile;
	private int iTrainID;
	
	public TrainCar(logFileIO lf, int tID)
	{
		logFile = lf;
		iTrainID = tID;
		
		logFileHeader = new String("Train " + iTrainID + ": TrainCar: ");
		logFile.write(logFileHeader + "I am a Train Car!");
		
		iNumPassengers = 0;
		bConductorOnBoard = false;
		dLength = TrainConstants.CAR_LEN;
		dWidth = TrainConstants.CAR_WIDTH;
		dHeight = TrainConstants.CAR_HEIGHT;
		dTotalWeight = TrainConstants.CAR_BASE_WEIGHT;
		dPassengerWeight = TrainConstants.PASSENGER_WEIGHT;
		
		iAdvertCount = 0;
		iAnnouncmentCount = 0;
		
		dThermostat = 70;
	}
	
	public double getThermostat()
	{
		return this.dThermostat;
	}
	
	public void setThermostat( double t )
	{
		this.dThermostat = t;
	}
	
	public String getCurrentAnnoucement()
	{
		return sCurrentAnnouncement;
	}
	
	public String getCurrentAdvert()
	{
		return sCurrentAdvert;
	}
	
	public void run()
	{
		iAdvertCount++;
		if(iAdvertCount > TrainConstants.TICKS_PER_ADVERT)
		{
			iAdvertCount = 0;
			sCurrentAdvert = null;
		}
		
		iAnnouncmentCount++;
		if(iAnnouncmentCount > TrainConstants.TICKS_PER_ADVERT)
		{
			iAnnouncmentCount = 0;
			sCurrentAnnouncement = null;
		}
	}
	
	public int getNumPassengers()
	{
		return iNumPassengers;
	}
	
	// Return the number of passengers that could't fit in the car
	public int setPassengerFlow( int iPassengerFlow )
	{
		logFile.write(logFileHeader + "Trying to load passengers: " + iPassengerFlow);
		if( !bDoorsOpen )
		{
			logFile.write(logFileHeader + "ERROR: ATTEMPTED TO LOAD PASSENGERS WITH DOORS CLOSED");
			return -1;
		}
		else if( (iNumPassengers + iPassengerFlow) > TrainConstants.MAX_PASSENGERS )
		{
			int iNumToAllow = TrainConstants.MAX_PASSENGERS - iNumPassengers;
			iNumPassengers += iNumToAllow;
			dTotalWeight += (dPassengerWeight * iNumToAllow);
			logFile.write(logFileHeader + "ERROR: ATTEMPTED TO LOAD TOO MANY PASSENGERS - ONLY ACCEPTED: " + iNumToAllow);
			
			return iPassengerFlow - iNumToAllow;
		}
		else if( (iNumPassengers + iPassengerFlow) < 0 )
		{
			logFile.write(logFileHeader + "ERROR: ATTEMPTED TO DISCHARGE TOO MANY PASSENGERS");
			
			dTotalWeight -= (dPassengerWeight*iNumPassengers);
			iNumPassengers = 0;
			return 0;
		}
		else
		{
			logFile.write(logFileHeader + "Successfully loaded passengers");
			dTotalWeight += (dPassengerWeight * iPassengerFlow);
			iNumPassengers += iPassengerFlow;
			return 0;
		}
	}
	
	public boolean getConductor()
	{
		return bConductorOnBoard;
	}
	
	public void loadConductor()
	{
		logFile.write(logFileHeader + "Conductor Loaded");
		dTotalWeight += dPassengerWeight;
		bConductorOnBoard = true;
	}
	
	public void removeConductor()
	{
		logFile.write(logFileHeader + "Conductor Unloaded");
		dTotalWeight -= dPassengerWeight;
		bConductorOnBoard = false;
	}
	
	public double getWeight()
	{
		return dTotalWeight;
	}
	
	public double getHeight()
	{
		return dHeight;
	}
	
	public double getLength()
	{
		return dLength;
	}
	
	public double getWidth()
	{
		return dWidth;
	}
	
	public boolean areLightsOn()
	{
		return bLightsOn;
	}
	
	public void turnLightsOn()
	{
		logFile.write(logFileHeader + "Turning Lights On");
		bLightsOn = true;
	}
	
	public void turnLightsOff()
	{
		logFile.write(logFileHeader + "Turning Lights Off");
		bLightsOn = false;
	}
	
	public boolean areDoorsOpen()
	{
		return bDoorsOpen;
	}
	
	public void closeDoors()
	{
		logFile.write(logFileHeader + "Closing Doors");
		bDoorsOpen = false;
	}
	
	public void openDoors()
	{
		logFile.write(logFileHeader + "Opening Doors");
		bDoorsOpen = true;
	}
	
	public void setAnnouncements( String s )
	{
		if( sCurrentAnnouncement == null )
		{
			sCurrentAnnouncement = s;
		}
	}
	
	public void setAdvertisements( String s )
	{
		if( sCurrentAdvert == null )
		{
			sCurrentAdvert = s;
		}
	}
}
